package com.ruoyi.deal.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.ruoyi.deal.domain.HyDealCategory;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.deal.domain.HyDealBrand;
import com.ruoyi.deal.domain.HyDealCategoryBrand;
import com.ruoyi.deal.mapper.HyDealCategoryBrandMapper;
import com.ruoyi.deal.service.IHyDealBrandService;
import com.ruoyi.deal.service.IHyDealCategoryBrandService;
import com.ruoyi.deal.service.IHyDealCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 分类管理Service业务层处理
 *
 * @author lixin
 * @date 2021-05-13
 */
@Service
public class HyDealCategoryBrandServiceImpl extends ServicePlusImpl<HyDealCategoryBrandMapper, HyDealCategoryBrand> implements IHyDealCategoryBrandService {

	@Autowired
	private IHyDealCategoryService categoryService;

	@Autowired
	private IHyDealBrandService brandService;

	@Autowired
	private IHyDealCategoryBrandService categoryBrandService;


	/**
	 * 加载分类对应品牌列表
	 * @param categoryId
	 * @return
	 */
	@Override
	public List<HyDealBrand> listByCategoryId(Long categoryId) {
		HyDealCategory category = categoryService.getById(categoryId);
		if(ObjectUtil.isNull(category)) {
			throw new CustomException("分类信息不存在");
		}

		LambdaQueryWrapper<HyDealCategoryBrand> lqw = Wrappers.lambdaQuery();
		lqw.eq(HyDealCategoryBrand::getCategoryId, category.getCategoryId());

		List<HyDealCategoryBrand> categoryBrandList = categoryBrandService.list(lqw);

		List<HyDealBrand> brandList = new ArrayList<>();
		for (HyDealCategoryBrand categoryBrand : categoryBrandList) {

			HyDealBrand brand = brandService.getOne(new QueryWrapper<HyDealBrand>()
				.select("brand_id", "brand_name")
				.eq("brand_id", categoryBrand.getBrandId()));
			brandList.add(brand);
		}

		return brandList;
	}
}
